﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using VehicleManagement.Models;
using VehicleManagement.Codes;

namespace VehicleManagement.ControllersServices
{
    public class CarPartsServices
    {
        CarInfoServices carServices = new CarInfoServices();
        DriverServices driverServices = new DriverServices();


        /// <summary>
        /// 根据车ID查车牌号、根据司机ID查司机姓名
        /// </summary>
        /// <param name="approval">车辆维修表信息对象</param>
        /// <returns>修改后的车辆维修表信息对象</returns>
        public CarParts GetDriverNameAndCarNumber(ref CarParts carparts)
        {
            carparts.CarNumber = carServices.GetCarNumByCarId(carparts.CarID);
            carparts.DriverName = driverServices.GetDriverNameByDriverID(carparts.DriverID);
            return carparts;
        }


        /// <summary>
        /// 根据车牌号查车ID，根据司机姓名查司机ID
        /// </summary>
        /// <param name="approval">车辆维修表信息对象</param>
        /// <returns>修改后的车辆维修表信息对象</returns>
        public CarParts GetDriverIDAndCarID(ref CarParts carparts)
        {
            carparts.CarID = carServices.GetCarIdByCarNum(carparts.CarNumber);
            carparts.DriverID = driverServices.GetDriverIdByDriverName(carparts.DriverName);
            return carparts;
        }



        /// <summary>
        /// 根据哈希表参数 模糊搜索车辆构件
        /// </summary>
        /// <param name="ht">哈希表参数</param>
        /// <returns>IList<CarParts></returns>
        public IList<CarParts> SearchCarParts(Hashtable ht)
        {
            IList<CarParts> lstCarParts = new List<CarParts> { };
            lstCarParts = SqlMapperManager.Instance.Get().QueryForList<CarParts>("SEARCH_CarParts", ht);
            return lstCarParts;
           
        }


        /// <summary>
        /// 获得所有的购件
        /// </summary>
        /// <returns>IList<CarParts></returns>
        public IList<CarParts> GetAllCarParts()
        {
            IList<CarParts> lstModel = SqlMapperManager.Instance.Get().QueryForList<CarParts>("SELECT_CARPARTSINFO", null);
            return lstModel;

        }

        public CarParts GetCarPartsByPartID(int id)
        {
            IList<CarParts> lstModel = SqlMapperManager.Instance.Get().QueryForList<CarParts>("SELECT_CARPART_BY_ID", id);
            CarParts carpart = lstModel.Where(u => u.PartID == id).First();
            GetDriverNameAndCarNumber(ref carpart);
            return carpart;
        }

        public object CreateCarPart(CarParts carpart)
        {
            GetDriverIDAndCarID(ref carpart);//根据司机姓名得到司机的ID、根据车牌号得到车辆ID
            return SqlMapperManager.Instance.Get().Insert("INSERT_CARPART", carpart);
        }

        public int EditCarPart(CarParts carpart)
        {

            GetDriverIDAndCarID(ref carpart);//根据司机姓名得到司机的ID、根据车牌号得到车辆ID
            int row = SqlMapperManager.Instance.Get().Update("UPDATE_CARPART", carpart);
            return row;
        }

        public int DeleteCarPart(int id)
        {
            int i = SqlMapperManager.Instance.Get().Delete("DELETE_CARPART", id);
            return i;
        }

        public IList<CarParts> SearchCarParts(Hashtable ht, int skip, int max)
        {
            IList<CarParts> lstCarParts  = SqlMapperManager.Instance.Get().QueryForList<CarParts>("SEARCH_CarParts", ht,skip,max);
            return lstCarParts;
        }

        public int CountAllCarParts(Hashtable ht)
        {
            return SearchCarParts(ht).Count;
        }
    }
}